package DAO;

import Connect.ConnectDB;
import java.awt.List;
import javax.swing.*;
import java.sql.*;
import java.util.Vector;
import javax.swing.table.DefaultTableModel;

public class VacFillDetailDAO {

    Connection conn;
    ResultSet rsVacFill = null;
    Statement st;
    PreparedStatement pst;

    public VacFillDetailDAO() {
        ConnectDB cdb = new ConnectDB();
        cdb.getConnect();
        this.conn = cdb.conn;

        loadDataVacFill();
    }

    public Vector loadVacFillToUpdate(String VacId) {
        Vector vecVacData = new Vector();
        try {
            rsVacFill.beforeFirst();

            while (rsVacFill.next()) {
                if (VacId.equals(rsVacFill.getString("VACANCYID"))) {

                    vecVacData.add(rsVacFill.getObject(1));
                    vecVacData.add(rsVacFill.getObject(2));
                    vecVacData.add(rsVacFill.getObject(3));
                    vecVacData.add(rsVacFill.getObject(4));

                }
            }

            return vecVacData;
        } catch (Exception e) {
            System.out.println("Error in loadVacFillToUpdateForm: " + e.getMessage());
            return null;
        }
    }

    public void loadDataVacFill() {
        try {
            String query = "SELECT "
                    + "             * "
                    + "     FROM "
                    + "             VacanncyFillDetails";
            st = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
            rsVacFill = st.executeQuery(query);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public String checkVacFillId(String EmpId) {
        try {
            loadDataVacFill();
            while (rsVacFill.next()) {
                if (rsVacFill.getString("VACANCYID").equals(EmpId)) {
                    return "Exist";
                }
            }
            return "Not exist";
        } catch (Exception e) {
            System.out.println("Error in checkEmpId: " + e.getMessage());
            return "Error";
        }
    }

    public Vector vacLoadVacId() {

        ResultSet rs = null;
        try {
            String query = "SELECT VACANCYID FROM Vacancies";
            PreparedStatement ps = conn.prepareStatement(query);
            rs = ps.executeQuery();
            Vector vec = new Vector();
            while (rs.next()) {
                vec.add(rs.getString(1));
            }
            return vec;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }

    }

    public Vector vacLoadEmpId() {
        ResultSet rs = null;
        try {
            String query = "SELECT EMPID FROM Employees";
            PreparedStatement ps = conn.prepareStatement(query);
            rs = ps.executeQuery();
            Vector vec = new Vector();
            while (rs.next()) {
                vec.add(rs.getString(1));
            }
            return vec;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        }
    }

    public void loadVacFillCombobox(JComboBox cbVac, JComboBox cbEmp) {
        Vector VacIds = vacLoadVacId();
        Vector EmpIds = vacLoadEmpId();

        cbVac.removeAllItems();
        cbEmp.removeAllItems();

        cbVac.addItem("Please select...");
        cbEmp.addItem("Please select...");

        for (int i = 0; i < VacIds.size(); i++) {
            cbVac.addItem(VacIds.get(i));
        }

        for (int i = 0; i < EmpIds.size(); i++) {
            cbEmp.addItem(EmpIds.get(i));
        }
    }

    public boolean insertVacFill(Vector VacFillData) {
        try {
            Vector vecVacFillData = VacFillData;
            while (rsVacFill.next()) {
                if (rsVacFill.getString("VACANCYID").equals(VacFillData.get(0)) && rsVacFill.getString("EMPID").equals(VacFillData.get(1))) {
                    System.out.println("Error in insertVacFill: Employee was registration Vacancy");
                    return false;
                } else {
                    rsVacFill.moveToInsertRow();
                    for (int i = 0; i < vecVacFillData.size(); i++) {
                        rsVacFill.updateString(i + 1, vecVacFillData.get(i).toString());
                    }
                    rsVacFill.insertRow();
                    loadDataVacFill();
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            System.out.println("Error in insertVacFill: " + e.getMessage());
            return false;
        }
    }

    public boolean updatateVacFill(Vector vecVacData) {
        try {
            rsVacFill.beforeFirst();
            while (rsVacFill.next()) {
                if (rsVacFill.getString("VACANCYID").equals(vecVacData.get(0))) {
                    for (int i = 0; i < vecVacData.size(); i++) {
                        rsVacFill.updateString(i + 1, vecVacData.get(i).toString());
                    }
                    rsVacFill.updateRow();
                    loadDataVacFill();
                    return true;
                }
            }

            return false;
        } catch (Exception e) {
            System.out.println("Error in updateVacFill: " + e.getMessage());
            return false;
        }
    }

    public boolean deleteVacFill(String VacId) {
        try {
            rsVacFill.beforeFirst();
            while (rsVacFill.next()) {
                if (rsVacFill.getString("VACANCYID").trim().equals(VacId)) {
                    rsVacFill.deleteRow();
                    loadDataVacFill();
                    return true;
                }
            }
            return false;
        } catch (Exception e) {
            System.out.println("Have data in orther table");
            return false;
        }
    }

    public DefaultTableModel loadTableVacFill() {
        try {
            String query = "Select * From VacanncyFillDetails";
            Statement ps = conn.createStatement();
            ResultSet rs = ps.executeQuery(query);

            Vector nameCol = new Vector();
            nameCol.add("Vacancy ID");
            nameCol.add("Employee ID");
            nameCol.add("Fill Date");
            nameCol.add("Intake Detail");


            DefaultTableModel dtm = new DefaultTableModel(nameCol, 0);

            while (rs.next()) {

                Vector dataRow = new Vector();
                dataRow.add(rs.getString("VACANCYID"));
                dataRow.add(rs.getString("EMPID"));
                dataRow.add(rs.getString("FILLEDDATE"));
                dataRow.add(rs.getString("INTAKEDETAILS"));
                dtm.addRow(dataRow);
            }
            return dtm;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}
